from datetime import datetime


class Frame:
    timeFormate = "%Y-%m-%d %H:%M:%S"

    def ConvertToList(self) -> list:
        return [
            self.time,
            self.illuminance,
            self.atmospher,
            self.humidity,
            self.temperature,
            self.voltage,
            self.current,
        ]

    def GetTitles(self) -> list[str]:
        return [
            "Time",
            "Illuminance",
            "Atmospher",
            "Humidity",
            "Temperature",
            "Voltage",
            "Current",
        ]

    # without \n
    def CodeToString(self) -> str:
        return (
            self.time.strftime(self.timeFormate)
            + ","
            + f"{self.illuminance:.2f},\
                {self.atmospher:.2f},\
                {self.humidity:.2f},\
                {self.temperature:.2f},\
                {self.voltage:.2f},\
                {self.current:.2f}"
        )

    
    def DecodeFromStringWithTime(self, valueStr: str, time: datetime):
        self.time = time
        valueStrs = valueStr.split(",")
        self.illuminance = float(valueStrs[0])
        self.atmospher = float(valueStrs[1])
        self.humidity = float(valueStrs[2])
        self.temperature = float(valueStrs[3])
        self.voltage = float(valueStrs[4])
        self.current = float(valueStrs[5])


    def DecodeFromString(self, valueStr: str):
        valueStrs = valueStr.split(",")
        self.time = datetime.strptime(valueStrs[0], self.timeFormate)
        self.illuminance = float(valueStrs[1])
        self.atmospher = float(valueStrs[2])
        self.humidity = float(valueStrs[3])
        self.temperature = float(valueStrs[4])
        self.voltage = float(valueStrs[5])
        self.current = float(valueStrs[6])

    # TODO: add encode and decode from binary
    def CodeToBinary(self) -> bytes:
        return self.CodeToString().encode("utf-8")

    # TODO: add encode and decode from binary
    def DecodeFromBinary(self, binaryData: bytes):
        self.DecodeFromString(binaryData.decode("utf-8"))

    def __str__(self):
        return "Time: {}, Illuminance: {}, Atmospher: {}, Humidity: {}, Temperature: {}, Voltage: {}, Current: {}".format(
            self.time,
            self.illuminance,
            self.atmospher,
            self.humidity,
            self.temperature,
            self.voltage,
            self.current,
        )



